package edu.mju.eduorder.controller;


import edu.mju.commonutils.ReturnCode;
import edu.mju.eduorder.service.StudyPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.logging.Handler;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author xiaoxin
 * @since 2020-10-05
 */
@RestController
@RequestMapping("/eduorder/study-pay-log")
//@CrossOrigin
public class StudyPayLogController {

    @Autowired
    private StudyPayLogService studyPayLogService;

    /**
     * 生成微信支付二维码接口
     * @param orderNo 订单号
     * @return json
     */
    @GetMapping("/createNative/{orderNo}")
    public ReturnCode createNative(@PathVariable String orderNo){
        Map<String,Object> map = studyPayLogService.createNative(orderNo);
        System.out.println("生成微信支付二维码接口map集合："+map);
        return ReturnCode.success().data(map);
    }


    /**
     *  查询订单支付状态
     * @param orderNo 订单编号
     * @return json
     */
    @GetMapping("/queryPayStatus/{orderNo}")
    public ReturnCode queryPayStatus(@PathVariable String orderNo){
        Map<String,String> map = studyPayLogService.queryPayStatus(orderNo);
        System.out.println("查询订单支付状态map集合："+map);
        if(map==null){
            return ReturnCode.fail().message("支付出错！");
        }

        //如果返回map里面不为空，通过map获取订单状态
        if(map.get("trade_state").equals("SUCCESS")){
            //向支付表加记录，更新订单表订单状态
            studyPayLogService.updateOrdersStatus(map);
            return ReturnCode.success();
        }

        return ReturnCode.success().code(25000).message("支付中");
    }

}

